let UserService = require('../service/UserService');

function login(context, username, password) {
	UserService.login(username, password)
		.then((data) => context.finish(data))
		.catch((error) => context.processError(error));
}

function register(context, username, password) {
	UserService.register(username, password)
		.then((data) => context.finish(data))
		.catch((error) => context.processError(error))
}

function userInfo(context) {
	let userId = context.user ? context.user._id : 0;
	UserService.getUserInfo(userId)
		.then(data => context.finish(data))
		.catch(error => context.processError(error));
}

function userList(context, page) {
	UserService.getUserList(page)
		.then((data) => context.finish(data))
		.catch((error) => context.processError(error));
}

let User = {
	login: login,
	register: register,
	userInfo: userInfo,
	userList: userList
};

module.exports = User;